//
// Created by tang on 23-3-25.
//

#include "icon_manager.hpp"

#include <content/image.hpp>

struct icon_info {
    int           code;
    ImageContent *image;
    const char   *asset_path;
};

#define ICON(code) {code, nullptr, "/assets/img/" #code ".png"}

static icon_info icons[] = {
        ICON(100),
        ICON(100),
        ICON(101),
        ICON(102),
        ICON(103),
        ICON(104),
        ICON(150),
        ICON(151),
        ICON(152),
        ICON(153),
        ICON(300),
        ICON(301),
        ICON(302),
        ICON(303),
        ICON(304),
        ICON(305),
        ICON(306),
        ICON(307),
        ICON(308),
        ICON(309),
        ICON(310),
        ICON(311),
        ICON(312),
        ICON(313),
        ICON(314),
        ICON(315),
        ICON(316),
        ICON(317),
        ICON(318),
        ICON(350),
        ICON(351),
        ICON(399),
        ICON(400),
        ICON(401),
        ICON(402),
        ICON(403),
        ICON(404),
        ICON(405),
        ICON(406),
        ICON(407),
        ICON(408),
        ICON(409),
        ICON(410),
        ICON(456),
        ICON(457),
        ICON(499),
        ICON(500),
        ICON(501),
        ICON(502),
        ICON(503),
        ICON(504),
        ICON(507),
        ICON(508),
        ICON(509),
        ICON(510),
        ICON(511),
        ICON(512),
        ICON(513),
        ICON(514),
        ICON(515),
        ICON(800),
        ICON(801),
        ICON(802),
        ICON(803),
        ICON(804),
        ICON(805),
        ICON(806),
        ICON(807),
        ICON(900),
        ICON(901),
        ICON(999),
        ICON(1001),
        ICON(1002),
        ICON(1003),
        ICON(1004),
        ICON(1005),
        ICON(1006),
        ICON(1007),
        ICON(1008),
        ICON(1009),
        ICON(1010),
        ICON(1011),
        ICON(1012),
        ICON(1013),
        ICON(1014),
        ICON(1015),
        ICON(1016),
        ICON(1017),
        ICON(1018),
        ICON(1019),
        ICON(1020),
        ICON(1021),
        ICON(1022),
        ICON(1023),
        ICON(1024),
        ICON(1025),
        ICON(1026),
        ICON(1027),
        ICON(1028),
        ICON(1029),
        ICON(1030),
        ICON(1031),
        ICON(1032),
        ICON(1033),
        ICON(1034),
        ICON(1035),
        ICON(1036),
        ICON(1037),
        ICON(1038),
        ICON(1039),
        ICON(1040),
        ICON(1041),
        ICON(1042),
        ICON(1043),
        ICON(1044),
        ICON(1045),
        ICON(1046),
        ICON(1047),
        ICON(1048),
        ICON(1049),
        ICON(1050),
        ICON(1051),
        ICON(1052),
        ICON(1053),
        ICON(1054),
        ICON(1055),
        ICON(1056),
        ICON(1057),
        ICON(1058),
        ICON(1059),
        ICON(1060),
        ICON(1061),
        ICON(1062),
        ICON(1063),
        ICON(1064),
        ICON(1065),
        ICON(1066),
        ICON(1067),
        ICON(1068),
        ICON(1069),
        ICON(1071),
        ICON(1072),
        ICON(1073),
        ICON(1074),
        ICON(1075),
        ICON(1076),
        ICON(1077),
        ICON(1078),
        ICON(1079),
        ICON(1080),
        ICON(1081),
        ICON(1082),
        ICON(1084),
        ICON(1085),
        ICON(1086),
        ICON(1087),
        ICON(1088),
        ICON(1089),
        ICON(1101),
        ICON(1302),
        ICON(1402),
        ICON(1601),
        ICON(1602),
        ICON(1603),
        ICON(1604),
        ICON(1605),
        ICON(1606),
        ICON(1607),
        ICON(1701),
        ICON(1702),
        ICON(1703),
        ICON(2001),
        ICON(2002),
        ICON(2003),
        ICON(2004),
        ICON(2005),
        ICON(2006),
        ICON(2007),
        ICON(2008),
        ICON(2009),
        ICON(2010),
        ICON(2011),
        ICON(2012),
        ICON(2013),
        ICON(2014),
        ICON(2015),
        ICON(2016),
        ICON(2017),
        ICON(2018),
        ICON(2019),
        ICON(2020),
        ICON(2021),
        ICON(2022),
        ICON(2023),
        ICON(2024),
        ICON(2025),
        ICON(2026),
        ICON(2027),
        ICON(2028),
        ICON(2052),
        ICON(2053),
        ICON(2054),
        ICON(9998),
        ICON(9999),
};
ImageContent *qweather_icon = nullptr;

IconManager::IconManager () {

}
IconManager::~IconManager () {
    free_icons();
}

void IconManager::load_icons () {
    for (auto &x: icons) {
        if (x.image == nullptr) {
            RESOURCE_IMAGE (x.image, x.asset_path);
        }
    }
    RESOURCE_IMAGE(qweather_icon, "/assets/img/qweather.png");  
}

void IconManager::free_icons () {
    for (auto &x: icons) {
        if (x.image) {
            delete x.image;
            x.image = nullptr;
        }
    }
    delete qweather_icon;
    qweather_icon = nullptr;
}

ImageContent *IconManager::icon (int code) const
{
    for (auto const &x: icons)
    {
        if (x.code == code)
            return x.image;
    }
    return qweather_icon;
}

ImageContent *IconManager::icon (const char *name) const
{
    char *end = nullptr;
    int id = strtol(name, &end, 10);
    if (end == nullptr)
        return qweather_icon;
    return icon(id);
}
